import { Micro } from "effect";

// Simulate fetching weather data
function fetchWeather(city: string): Promise<string> {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (city === "London") {
        resolve("Sunny");
      } else {
        reject(new Error("Weather data not found for this location"));
      }
    }, 1_000);
  });
}

function getWeather(city: string) {
  return Micro.promise(() => fetchWeather(city));
}

//      ┌─── Micro<string, never, never>
//      ▼
const weatherEffect = getWeather("London");

Micro.runPromise(weatherEffect)
  .then((data) => console.log(`The weather in London is: ${data}`))
  .catch((error: Error) =>
    console.error(`Failed to fetch weather data: ${error.message}`),
  );
/*
Output:
The weather in London is: Sunny
*/
